#include "flash.h"
#include "g.h"

void flash_dataInit(void);
void flash_analysis(void);
#define FLASH_PAGE_SIZE        ((uint16_t)0x200)

#define FLASH_PUlSE_START_ADDR ((uint32_t)0x0800fa00)
#define FLASH_WRITE_START_ADDR1 ((uint32_t)FLASH_PUlSE_START_ADDR+FLASH_PAGE_SIZE)
#define FLASH_WRITE_START_ADDR2 ((uint32_t)(FLASH_WRITE_START_ADDR1+FLASH_PAGE_SIZE))
#define FLASH_WRITE_END_ADDR   ((uint32_t)0x08010000)



uint16_t suncheck(uint8_t *data,uint16_t len)
{
	uint16_t sunt = 0;
	int i = 0;
	
	for( i = 0;i < len;i++)
	{
		sunt -=data[i];
	}
	
	return sunt;
	
}

void flash_EraseOnePage(uint32_t  addr)
{

	FLASH_Unlock();
	FLASH_EraseOnePage(addr);
	FLASH_Lock();
}

void flash_write_only(uint32_t addr,uint32_t *pbuff,uint8_t len)
{
	uint8_t i = 0;
	FLASH_Unlock();
	for(i = 0; i <len;i++)
	{
		FLASH_ProgramWord(addr + i*4, pbuff[i]);
	}
	FLASH_Lock();
}


void flash_write(uint32_t *pbuff,uint8_t len)
{
	uint8_t i = 0;
	FLASH_Unlock();
	FLASH_EraseOnePage(FLASH_WRITE_START_ADDR1);
	FLASH_EraseOnePage(FLASH_WRITE_START_ADDR2);
	for(i = 0; i <len;i++)
	{
		FLASH_ProgramWord(FLASH_WRITE_START_ADDR1 + i*4, pbuff[i]);
	}
	FLASH_Lock();
}
system_save_parameter_t  save_parameter;
void flash_load(void)
{
	uint16_t sum = 0;
	uint8_t *p = (uint8_t *)FLASH_WRITE_START_ADDR1;
	
	memset(&save_parameter,0,sizeof(save_parameter));
		
	
	memcpy(&save_parameter,p,sizeof(system_save_parameter_t));
	
	sum = suncheck((uint8_t*)(&save_parameter.systemparameter),(uint16_t)sizeof(save_parameter.systemparameter));
	if(sum == save_parameter.sum)
	{
		g_system.systemparameter  = save_parameter.systemparameter;
		flash_analysis();
		
		if(g_system.systemparameter.topPulse == 0 && g_system.systemparameter.bottonPulse == 0)
		{
			g_system.journeylearn.result = JOURNEYLEARN_FAIL;
		}
	}
	else
	{
		flash_dataInit();
	}
}


void flash_run(void)
{
	uint16_t len = 0;
//	system_save_parameter_t  save_parameter;
	
	memset(&save_parameter,0,sizeof(save_parameter));
	if(g_system.flashSave == 1)
	{	
		save_parameter.systemparameter = g_system.systemparameter;
		
		save_parameter.sum = suncheck((uint8_t*)(&save_parameter.systemparameter),(uint16_t)sizeof(save_parameter.systemparameter));
				
		if(sizeof(system_save_parameter_t)%4 == 0)
		{
			len = sizeof(system_save_parameter_t)/4;
		}
		else
		{
			len = sizeof(system_save_parameter_t)/4+1;
		}
		
		flash_write((uint32_t*)(&save_parameter),len);
		
		g_system.flashSave = 0;
	}
}

void flash_dataInit(void)
{
	g_system.journeylearn.result = JOURNEYLEARN_FAIL;
	g_system.systemparameter.CloseForce = 5-1;
	g_system.systemparameter.infraredEnable = 0;
	g_system.systemparameter.autoclose = 0;
	g_system.systemparameter.autolock = 0;
	g_system.systemparameter.openForce = 9-1;
	g_system.systemparameter.ventilate = 0;
	g_system.systemparameter.flow = 0;
	g_system.systemparameter.maintain = 0;
	g_system.systemparameter.openspeed = 5;
	g_system.systemparameter.closespeed = 5;
	g_system.systemparameter.electronicLock = 0;
	g_system.systemparameter.Courtyard =0;
	g_system.systemparameter.standbyMode = 0;
	g_system.systemparameter.counter = 0;
	g_system.systemparameter.stopButtonSwitch = 0;
	flash_analysis();
	g_system.flashSave = 1;
}

void flash_analysis(void)
{
	if(g_system.systemparameter.openspeed<=5)
	{
		motorControlSpeedSet(0,g_system.systemparameter.openspeed);
	}
	else
	{
		motorControlSpeedSet(0,5);
	}
	
	if(g_system.systemparameter.closespeed<=5)
	{
		motorControlSpeedSet(1,g_system.systemparameter.closespeed);
	}
	else
	{
		motorControlSpeedSet(1,5);
	}
	
	g_system.moto_parameter.pulse = g_system.systemparameter.curPulse;
}

void flash_pulseload(void)
{
	uint32_t *p = (uint32_t *)FLASH_PUlSE_START_ADDR;
	uint16_t len = FLASH_PAGE_SIZE/4;
	uint32_t t_plush;
	int i =0;
	
	for(i = 0;i<len;i++)
	{
		if(*(p+i)==UINT32_MAX)
			break;
	}
	
	if(i == len)
	{ 
		g_system.moto_parameter.pulse = 0;
	}
	else
	{
		t_plush = (*(p+i-1))&0xffff;		
		g_system.moto_parameter.pulse = t_plush;
	}
}

uint8_t flash_pulsehwrite(void)
{
	uint32_t *p = (uint32_t *)FLASH_PUlSE_START_ADDR;
	uint16_t len = FLASH_PAGE_SIZE/4;
	uint32_t temp_t = 0xffff;
	int i =0;
	
	
	temp_t = temp_t&g_system.moto_parameter.pulse;
	for(i = 0;i<len;i++)
	{
		if(*(p+i)==UINT32_MAX)
			break;
	}
	//printf("flash_pulsehwrite %d\r\n",temp_t);
	if(i == len)
	{
		FLASH_Unlock();
		FLASH_EraseOnePage(FLASH_PUlSE_START_ADDR);
		FLASH_ProgramWord(FLASH_PUlSE_START_ADDR,temp_t);
		FLASH_Lock();
		
		if(*p == g_system.moto_parameter.pulse)
			return 1;
		else 
			return 0;
		
	}
	else
	{
		FLASH_Unlock();
		FLASH_ProgramWord(FLASH_PUlSE_START_ADDR + i*4, temp_t);
		FLASH_Lock();
		
		p =(uint32_t*)(FLASH_PUlSE_START_ADDR + i*4);
		
		
		if(*p == temp_t)
			return 1;
		else 
			return 0;
	}
	
	
	
	
}



